In Svelte projects, organizing code into lib, routes, and hooks helps maintain clarity, reusability, and scalability. Each serves a distinct purpose in structuring your application.
lib/ is used for reusable code like components, stores for state management, and utility functions. It promotes modularity and avoids code duplication.
routes/ contains page components and defines the URL structure of the app. Nested folders correspond to nested routes. Files like +page.svelte or +page.js handle the page UI and data fetching.
hooks.server.js and hooks.client.js allow running code before or after requests. Server hooks are useful for authentication, logging, or modifying requests/responses. Client hooks can initialize global client-side logic or behaviors.
lib/ – reusable components, stores, and utilities.
routes/ – defines app pages and URL structure.
hooks.server.js – server-side request handling (auth, logging, etc.).
hooks.client.js – client-side initialization or global behaviors.